package com.reder.practice.service.impl;

import com.reder.practice.bean.Comment;
import com.reder.practice.bean.User;
import com.reder.practice.bean.Video;
import com.reder.practice.common.dto.ServerResponse;
import com.reder.practice.common.enums.state.CommentState;
import com.reder.practice.common.enums.state.CommonState;
import com.reder.practice.common.enums.state.VideoState;
import com.reder.practice.common.util.PageUtil;
import com.reder.practice.common.util.SqlResultUtil;
import com.reder.practice.dao.CommentDao;
import com.reder.practice.service.CommentService;
import com.reder.practice.service.VideoService;
import com.reder.practice.vo.CommentVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 评论业务实现类
 *
 * @author Red
 * <p>Email: 1318944013@qq.com</p>
 * <p>created by 2019/09/23 9:39</p>
 */
@Service
@Slf4j
public class CommentServiceImpl implements CommentService {

    @Resource
    private CommentDao commentDao;
    @Resource
    private VideoService videoService;

    // 视频id -> 该视频的评论列表
    @Override
    public ServerResponse listComment(long videoId, int pageNumber, int sizeOfOnePage) {
        // 分页开始
        PageUtil.toPage(pageNumber, sizeOfOnePage);
        // 查询评论列表
        List<CommentVo> list = commentDao.queryByVideoId(videoId);
        return ServerResponse.response(CommonState.SUCCESS, PageUtil.pageInfo(list));
    }

    // 新增评论
    @Override
    @Transactional
    public ServerResponse insertComment(Comment comment, User user) {
        // 判断参数是否完整
        if (!commentParamIsRight(comment)) {
            return ServerResponse.response(CommentState.COMMENT_PARAM_ERROR);
        }
        // 设置必要的属性
        comment.setCreateTime(new Date());
        comment.setUserId(user.getId());
        // 更新视频的评论数
        ServerResponse response = incrCommentNum(comment.getVideoId());
        if (!response.isSuccess()) {
            return response;
        }
        // 评论 -> 数据库
        return SqlResultUtil.checkSqlResult(commentDao.insert(comment),
                CommentState.COMMENT_ADD_ERROR);
    }

    // 删除评论
    @Override
    @Transactional
    public ServerResponse deleteComment(long commentId) {
        // 判断评论是否存在
        Comment comment = commentDao.selectByPrimaryKey(commentId);
        if (comment == null) {
            return ServerResponse.response(CommentState.COMMENT_IS_NOT_EXIST);
        }
        // 更新视频的评论数
        ServerResponse response = descCommentNum(comment.getVideoId());
        if (!response.isSuccess()) {
            return response;
        }
        return SqlResultUtil.checkSqlResult(commentDao.deleteByPrimaryKey(commentId),
                CommentState.COMMENT_DELETE_ERROR);
    }

    // 判断评论参数是否正常
    private boolean commentParamIsRight(Comment comment) {
        if (comment == null || comment.getVideoId() == null
                || comment.getContent() == null) {
            log.error("comment参数不完整, comment是{}", comment);
            return false;
        }
        return true;
    }

    // 评论数+1
    private ServerResponse incrCommentNum(long videoId) {
            return SqlResultUtil.checkSqlResult(commentDao.incrCommentNum(videoId),
                    VideoState.UPDATE_VIDEO_ERROR);

    }

    // 评论数-1
    private ServerResponse descCommentNum(long videoId) {
        return SqlResultUtil.checkSqlResult(commentDao.descCommentNum(videoId),
                VideoState.UPDATE_VIDEO_ERROR);

    }


}
